﻿using System;
using SmartSiteManagementSystem.IRepository.Admin;
using SmartSiteManagementSystem.Models.Admin;
using SmartSiteManagementSystem.Repository.DBHelper;
using System.Data;
using Dapper;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using MySql.Data.MySqlClient;

namespace SmartSiteManagementSystem.Repository.Admin
{
    public class AdminGroupInfoRepository : IAdminGroupInfoRepository
    //,IDisposable
    {
        #region dataTable
        /*
 create table `admin_group`( 
	`group_id` int(11) not null AUTO_INCREMENT primary key, 
	`group_name` varchar(50) not null ,
	`code` varchar(50) null,
	`email` varchar(50) null,
	`sort_order` int not null,
	`is_stop` bit not null default 0,
	`is_deleted`  bit not null default 0,
	`remark` varchar(250) null,
	`create_time` datetime not null default now(),
	`update_time` datetime not null default now()
    ) default charset=utf8;
         */
        #endregion
       // private IDbConnection _readConn;
        //private IDbConnection _writeConn;

        public bool Delete(Hashtable param)
        {
            bool back = false;
            if (param == null)
                param = new Hashtable();
            String querySql = @"delete from admin_group";
            using (IDbConnection _writeConn = new MySqlConnection(DbHelperOptions.WriteConnectionString))
            {

                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where ");
                    int parCount = 0;
                    if (param.ContainsKey("group_id"))
                    {
                        pars.Add("group_id", param["group_id"]);

                        whereCondition.Append(" group_id =@group_id");
                        parCount++;
                    }
                    if (param.ContainsKey("group_name"))
                    {
                        pars.Add("group_name", param["group_name"]);
                        if(parCount>0)
                        {
                            whereCondition.Append(" and ");
                        }
                        whereCondition.Append("group_name =@group_name");
                        parCount++;
                    }
                    if (param.ContainsKey("code"))
                    {
                        pars.Add("code", param["code"]);
                        if (parCount > 0)
                        {
                            whereCondition.Append(" and ");
                        }
                        whereCondition.Append("code =@code");
                        parCount++;
                    }
                    if (param.ContainsKey("is_deleted"))
                    {
                        pars.Add("is_deleted", param["is_deleted"]);
                        if (parCount > 0)
                        {
                            whereCondition.Append(" and ");
                        }
                        whereCondition.Append("is_deleted =@is_deleted");
                        parCount++;
                    }
                }
                string execSql = querySql + whereCondition.ToString();
                int backInt = _writeConn.Execute(execSql, pars);
                if(backInt>0)
                {
                    back = true;
                }
            }
            return back;
        }

        public AdminGroupInfo Get(Hashtable param)
        {
            AdminGroupInfo back = null;
            if (param == null)
                param = new Hashtable();
            String querySql = @"select group_id as AdminUserGroupId, group_name as AdminUserGroupName,code as Code,email as Email,sort_order as SortOrder,is_stop as StopFlag,is_deleted as DeleteFlag,remark as Remark,create_time as CreateTime,update_time as UpdateTime from admin_group";
            using (IDbConnection _readConn = new MySqlConnection(DbHelperOptions.ReadConnectionString))
            {

                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where 1=1");
                    if (param.ContainsKey("group_id"))
                    {
                        pars.Add("group_id", param["group_id"]);
                        whereCondition.Append(" and group_id =@group_id");
                    }
                    if (param.ContainsKey("group_name"))
                    {
                        pars.Add("group_name", param["group_name"]);
                        whereCondition.Append(" and group_name =@group_name");
                    }
                    if (param.ContainsKey("code"))
                    {
                        pars.Add("code", param["code"]);
                        whereCondition.Append(" and code =@code");
                    }
                    if (param.ContainsKey("is_deleted"))
                    {
                        pars.Add("is_deleted", param["is_deleted"]);
                        whereCondition.Append(" and is_deleted =@is_deleted");
                    }
                }
                whereCondition.Append(" limit 1");
                string execSql = querySql + whereCondition.ToString();
                back = _readConn.QueryFirstOrDefault<AdminGroupInfo>(execSql, pars);
            }
            return back;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public int GetCount(Hashtable param)
        {
            int back = 0;
            if (param == null)
                param = new Hashtable();
            String querySql = "select count(group_id) from admin_group";
            using (IDbConnection _readConn = new MySqlConnection(DbHelperOptions.ReadConnectionString))
            {
                #region
                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where 1=1");
                    if (param.ContainsKey("group_id"))
                    {
                        pars.Add("group_id", param["group_id"]);
                        whereCondition.Append("and group_id =@group_id");
                    }
                    if (param.ContainsKey("group_name"))
                    {
                        pars.Add("group_name", param["group_name"]);
                        whereCondition.Append(" and group_name =@group_name");
                    }
                    if (param.ContainsKey("code"))
                    {
                        pars.Add("code", param["code"]);
                        whereCondition.Append(" and code =@code");
                    }
                    if (param.ContainsKey("is_deleted"))
                    {
                        pars.Add("is_deleted", param["is_deleted"]);
                        whereCondition.Append(" and is_deleted =@is_deleted");
                    }
                }
                
                string execSql = querySql + whereCondition.ToString();
                #endregion
                back = _readConn.QueryFirstOrDefault<int>(execSql, pars);
            }
            return back;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="param"></param>
        /// <param name="orderBy"></param>
        /// <param name="first"></param>
        /// <param name="rows"></param>
        /// <returns></returns>
        public IList<AdminGroupInfo> GetList(Hashtable param, string orderBy, int first, int rows)
        {
            IList<AdminGroupInfo> back = null;
            if (param == null)
                param = new Hashtable();
            String querySql = @"select group_id as AdminUserGroupId, group_name as AdminUserGroupName,code as Code,email as Email,sort_order as SortOrder,is_stop as StopFlag,is_deleted as DeleteFlag,remark as Remark,create_time as CreateTime,update_time as UpdateTime from admin_group";
            using (IDbConnection _readConn = new MySqlConnection(DbHelperOptions.ReadConnectionString))
            {
                #region
                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where 1=1");
                    if (param.ContainsKey("group_id"))
                    {
                        pars.Add("group_id", param["group_id"]);
                        whereCondition.Append("and group_id =@group_id");
                    }
                    if (param.ContainsKey("group_name"))
                    {
                        pars.Add("group_name", param["group_name"]);
                        whereCondition.Append(" and group_name =@group_name");
                    }
                    if (param.ContainsKey("code"))
                    {
                        pars.Add("code", param["code"]);
                        whereCondition.Append(" and code =@code");
                    }
                    if (param.ContainsKey("is_deleted"))
                    {
                        pars.Add("is_deleted", param["is_deleted"]);
                        whereCondition.Append(" and is_deleted =@is_deleted");
                    }
                    if (param.ContainsKey("Ids"))
                    {
                        pars.Add("Ids", param["Ids"]);
                        whereCondition.Append(" and group_id in (@Ids)");

                    }
                }
                if (string.IsNullOrEmpty(orderBy))
                {
                    whereCondition.Append(" order by group_id desc");
                }
                else
                {
                    whereCondition.Append(" order by @orderBy");
                    pars.Add("orderBy", orderBy);
                }
                whereCondition.Append(" limit @first,@rows");
                pars.Add("first", first);
                pars.Add("rows", rows);
                #endregion
                string execSql = querySql + whereCondition.ToString();
                back = _readConn.Query<AdminGroupInfo>(execSql, pars).AsList<AdminGroupInfo>();
            }
            return back;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int Insert(AdminGroupInfo obj)
        {
            int back = 0;
            #region sql
            string sql = @"insert into admin_group (
group_name,
code,
email,
sort_order,
is_stop,
is_deleted,
remark,
create_time,
update_time
) VALUES(
      @group_name
      , @code
      , @email
      , @sort_order
      , @is_stop
      , @is_deleted
      , @remark
      , @create_time
      , @update_time
      );select last_insert_id();";
            #endregion
            using (IDbConnection _writeConn = new MySqlConnection(DbHelperOptions.WriteConnectionString))
            {

                back = _writeConn.QueryFirst<int>(sql, new
                {
                    group_name = obj.AdminUserGroupName,
                    code = obj.Code,
                    email = obj.Email,
                    sort_order = obj.SortOrder ,
                    is_stop = obj.StopFlag ,
                    is_deleted = obj.DeleteFlag,
                    remark = obj.Remark,
                    create_time = obj.CreateTime,
                    update_time = obj.UpdateTime
                });
            }
            return back;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool Update(AdminGroupInfo obj)
        {
            bool back = false;
            #region sql
            string sql = @"update admin_group
 SET
group_name = @group_name,
code = @code,
email = @email,
sort_order = @sort_order,
is_stop= @is_stop,
is_deleted = @is_deleted,
remark = @remark,
create_time = @create_time,
update_time = @update_time
WHERE group_id =@group_id;";
            #endregion
            using (IDbConnection _writeConn = new MySqlConnection(DbHelperOptions.WriteConnectionString))
            {

               int backint = _writeConn.Execute(sql, new
                {
                    group_id= obj.AdminUserGroupId,
                    group_name = obj.AdminUserGroupName,
                    code = obj.Code,
                    email = obj.Email,
                    sort_order = obj.SortOrder,
                    is_stop = obj.StopFlag,
                    is_deleted = obj.DeleteFlag,
                    remark = obj.Remark,
                    create_time = obj.CreateTime,
                    update_time = obj.UpdateTime
                });
                if(backint>0)
                {
                    back = true;
                }
            }
            return back;
        }
    }
}

